﻿// 开发者:micropu
// 公司:佛山远钧智慧科技有限公司

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Admin.NET.Application;
public static class CreateCmd
{
    public static byte[] CreateCmdLogin(string manufacturerId, string deviceId)
    {
        return new MainData(843, CreateContent.CreateLoginContent(manufacturerId, deviceId)).GetBytes();
    }

    public static byte[] CreateCmdHearbeat()
    {
        return new MainData(65535, Array.Empty<byte>()).GetBytes();
    }

    public static byte[] CreateCmdWhitelist()
    {
        return new MainData(849, Array.Empty<byte>()).GetBytes();
    }
    public static byte[] CreateCmdEmp(string DeviceId, string IdentityCard)
    {
        return new MainData(845, CreateContent.CreateEmpContent(DeviceId, IdentityCard)).GetBytes();
    }

    public static byte[] CreateCmdUploadEmp(string WorkerID, string cap_time, string PhotoData)
    {
        return new MainData(848, CreateContent.CreateUploadEmpContent(WorkerID, cap_time, PhotoData)).GetBytes();
    }
}

public static class CreateContent
{
    public static byte[] CreateLoginContent(string manufacturerId, string deviceId)
    {
        List<byte> bytes = new List<byte>();
        byte[] manufacturerIdBytes = Encoding.ASCII.GetBytes(manufacturerId);
        byte[] deviceIdBytes = Encoding.ASCII.GetBytes(deviceId);
        // 确保字节数组的长度符合预期
        manufacturerIdBytes = EnsureLength(manufacturerIdBytes, 32);
        deviceIdBytes = EnsureLength(deviceIdBytes, 32);

        bytes.AddRange(manufacturerIdBytes);
        bytes.AddRange(deviceIdBytes);
        // 计算校验和
        byte checksum = CalculateCheckByteSum(bytes.ToArray());
        bytes.Add(checksum);
        return bytes.ToArray();
    }

    public static byte[] CreateEmpContent(string DeviceId, string IdentityCard)
    {
        List<byte> bytes = new List<byte>();
        byte[] DeviceIdBytes = Encoding.ASCII.GetBytes(DeviceId);
        byte[] IdentityCardBytes = Encoding.ASCII.GetBytes(IdentityCard);
        // 确保字节数组的长度符合预期
        DeviceIdBytes = EnsureLength(DeviceIdBytes, 32);
        IdentityCardBytes = EnsureLength(IdentityCardBytes, 18);

        bytes.AddRange(DeviceIdBytes);
        bytes.AddRange(IdentityCardBytes);
        // 计算校验和
        byte checksum = CalculateCheckByteSum(bytes.ToArray());
        bytes.Add(checksum);
        return bytes.ToArray();
    }

    public static byte[] CreateUploadEmpContent(string workerID, string cap_time, string photoData)
    {
        List<byte> bytes = new List<byte>();
        byte[] workerIDBytes = BitConverter.GetBytes(Convert.ToUInt32(workerID));
        byte[] capTimeBytes = Convert.ToDateTime(cap_time).ToString("yyyyMMddHHmmss").ByHexStringToBytes();
        byte[] photoDataBytes = Convert.FromBase64String(photoData);
        byte[] photoLength = BitConverter.GetBytes(photoDataBytes.Length);

        bytes.AddRange(workerIDBytes);
        bytes.AddRange(capTimeBytes);
        bytes.Add((byte)0x06);
        bytes.AddRange(photoLength);
        bytes.AddRange(photoDataBytes);
        // 计算校验和
        byte checksum = CalculateCheckByteSum(bytes.ToArray());
        bytes.Add(checksum);
        return bytes.ToArray();
    }
}

public static class upPacket
{
    /// <summary>
    /// 解析白名单信息
    /// </summary>
    /// <param name="bytes"></param>
    /// <returns></returns>
    public static Whitelist upPacketWhiteList(byte[] bytes)
    {
        var whitelist = new Whitelist();

        int currentIndex = 0;
        // 读取指定字节数并递增索引的辅助函数
        byte[] ReadBytes(int count)
        {
            byte[] result = new byte[count]; // 按指定长度创建新字节数组
            Array.Copy(bytes, currentIndex, result, 0, count); // 复制指定长度的字节
            currentIndex += count;
            return result;
        }

        whitelist.ProjectNumber = BitConverter.ToUInt32(ReadBytes(4));
        whitelist.ProjectName = Encoding.UTF8.GetString(ReadBytes(100)).TrimEnd('\0');
        whitelist.Records = new List<DataRecord>();
        while (currentIndex + 41 <= bytes.Length)
        {
            var record = new DataRecord();
            record.WorkerNumber = BitConverter.ToUInt32(ReadBytes(4));
            record.WorkerName = Encoding.UTF8.GetString(ReadBytes(10)).TrimEnd('\0');
            record.IDNumber = Encoding.ASCII.GetString(ReadBytes(18)).TrimEnd('\0');
            record.PhotoLength = BitConverter.ToUInt32(ReadBytes(9), 0);
            whitelist.Records.Add(record);
        }
        return whitelist;
    }
}

